﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;
namespace KPIS.GERP.GFMS.DAL
{
    public class D_FIM_MAS_BorrowTypes : IDAL_FIM_MAS_BorrowTypes
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        //

        public IList<M_FIM_MAS_BorrowTypes> GetFull(Nullable<int> bgm_source_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_BorrowTypes> infoList = new List<M_FIM_MAS_BorrowTypes>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT bt.borrow_type_seq, bt.borrow_type_name, bt.bgm_source_seq"
                    + ", bt.cal_date_type, bt.return_day, s.bgm_source_name"
                    + ", case when bt.cal_date_type = '1' then 'เริ่มนับตั้งแต่วันที่รับเงิน'"
                        + " else 'เริ่มนับตั้งแต่วันที่สิ้นสุด'"
                        + " end as cal_date_type_name"
                    + ", bt.is_travel"

                    + ", bt.record_status, bt.created_by, bt.created_when, bt.updated_by, bt.updated_when"
                    + " FROM fim_mas_borrow_types bt"
                    + " LEFT JOIN bgm_mas_source s ON bt.bgm_source_seq = s.bgm_source_seq"
                    + " WHERE bt.record_status not like @delete_status"
                    + (bgm_source_seq != null ? " and bt.bgm_source_seq = @bgm_source_seq" : "")
                    + " ORDER BY bt.borrow_type_seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_BorrowTypes info = new M_FIM_MAS_BorrowTypes();
                        info.borrow_type_seq = Convert.ToInt32(drc[i]["borrow_type_seq"]);
                        info.borrow_type_name = drc[i]["borrow_type_name"].ToString();
                        info.bgm_source_seq = cn.ConvertNullInt(drc[i]["bgm_source_seq"]);
                        info.bgm_source_name = drc[i]["bgm_source_name"].ToString();
                        info.cal_date_type = drc[i]["cal_date_type"].ToString();
                        info.cal_date_type_name = drc[i]["cal_date_type_name"].ToString();
                        info.return_day = cn.ConvertNullInt(drc[i]["return_day"]);
                        info.is_travel = drc[i]["is_travel"].ToString();

                        info.record_status = drc[i]["record_status"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_FIM_MAS_BorrowTypes> GetList(Nullable<int> bgm_source_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_BorrowTypes> infoList = new List<M_FIM_MAS_BorrowTypes>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT borrow_type_seq, borrow_type_name"
                    + " FROM fim_mas_borrow_types"
                    + " WHERE record_status not like @delete_status"
                    + (bgm_source_seq != null ? " and bgm_source_seq = @bgm_source_seq" : "")
                    + " ORDER BY borrow_type_seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_BorrowTypes info = new M_FIM_MAS_BorrowTypes();
                        info.borrow_type_seq = Convert.ToInt32(drc[i]["borrow_type_seq"]);
                        info.borrow_type_name = drc[i]["borrow_type_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_FIM_MAS_BorrowTypes GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_FIM_MAS_BorrowTypes info = new M_FIM_MAS_BorrowTypes();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT bt.borrow_type_seq, bt.borrow_type_name, bt.bgm_source_seq"
                    + ", bt.cal_date_type, bt.return_day, s.bgm_source_name"
                    + ", case when bt.cal_date_type = '1' then 'เริ่มนับตั้งแต่วันที่เริ่ม'"
                        + " else 'เริ่มนับตั้งแต่วันที่สิ้นสุด'"
                        + " end as cal_date_type_name"
                    + ", bt.is_travel"

                    + ", bt.record_status, bt.created_by, bt.created_when, bt.updated_by, bt.updated_when"
                    + " FROM fim_mas_borrow_types bt"
                    + " LEFT JOIN bgm_mas_source s ON bt.bgm_source_seq = s.bgm_source_seq"
                    + " WHERE bt.borrow_type_seq = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.borrow_type_seq = Convert.ToInt32(drc[0]["borrow_type_seq"]);
                    info.borrow_type_name = drc[0]["borrow_type_name"].ToString();
                    info.bgm_source_seq = cn.ConvertNullInt(drc[0]["bgm_source_seq"]);
                    info.bgm_source_name = drc[0]["bgm_source_name"].ToString();
                    info.cal_date_type = drc[0]["cal_date_type"].ToString();
                    info.cal_date_type_name = drc[0]["cal_date_type_name"].ToString();
                    info.return_day = cn.ConvertNullInt(drc[0]["return_day"]);
                    info.is_travel = drc[0]["is_travel"].ToString();

                    info.record_status = drc[0]["record_status"].ToString();
                    info.created_by = cn.ConvertNullInt(drc[0]["created_by"]);
                    info.created_when = cn.ConvertNullDateTime(drc[0]["created_when"]);
                    info.updated_by = cn.ConvertNullInt(drc[0]["updated_by"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["updated_when"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(M_FIM_MAS_BorrowTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@borrow_type_seq", info.borrow_type_seq);
                dbManager.AddParameters(1, "@borrow_type_name", info.borrow_type_name);
                dbManager.AddParameters(2, "@bgm_source_seq", info.bgm_source_seq);
                dbManager.AddParameters(3, "@cal_date_type", info.cal_date_type);
                dbManager.AddParameters(4, "@return_day", info.return_day);
                dbManager.AddParameters(5, "@is_travel", info.is_travel);

                dbManager.AddParameters(6, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(7, "@created_by", info.created_by);
                dbManager.AddParameters(8, "@created_when", info.created_when);

                strSQL = "INSERT INTO fim_mas_borrow_types"
                    + " (borrow_type_name, bgm_source_seq, cal_date_type, return_day, is_travel"
                    + ", record_status, created_by, created_when)"
                    + " VALUES(@borrow_type_name"
                    + ", @bgm_source_seq"
                    + ", @cal_date_type"
                    + ", @return_day"
                    + ", @is_travel"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when"
                    + " )";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_FIM_MAS_BorrowTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@borrow_type_seq", info.borrow_type_seq);
                dbManager.AddParameters(1, "@borrow_type_name", info.borrow_type_name);
                dbManager.AddParameters(2, "@bgm_source_seq", info.bgm_source_seq);
                dbManager.AddParameters(3, "@cal_date_type", info.cal_date_type);
                dbManager.AddParameters(4, "@return_day", info.return_day);
                dbManager.AddParameters(5, "@is_travel", info.is_travel);

                dbManager.AddParameters(6, "@updated_by", info.updated_by);
                dbManager.AddParameters(7, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_borrow_types"
                    + " SET borrow_type_name = @borrow_type_name"
                    + ", bgm_source_seq = @bgm_source_seq"
                    + ", cal_date_type = @cal_date_type"
                    + ", return_day = @return_day"
                    + ", is_travel = @is_travel"
                    + ", updated_by = @Updated_by"
                    + ", updated_when = @Updated_when"
                    + " WHERE borrow_type_seq = @borrow_type_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(M_FIM_MAS_BorrowTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@borrow_type_seq", info.borrow_type_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_borrow_types"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE borrow_type_seq = @borrow_type_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}